package practice;

import java.util.Arrays;

public class guibingsort {
    public static void merge(int []arr,int left,int mid ,int right,int[] temp)
    {
        int l = left;
        int r = mid+1;
        int t= 0;
        while (l<=mid&&r<=right)
        {
            if(arr[l]<arr[r])
            {
                temp[t] = arr[l];
                t++;
                l++;
            }else
            {
                temp[t] =arr[r];
                t++;
                r++;
            }
        }
        while (l<=mid)
        {
            temp[t]= arr[l];
            t++;
            l++;
        }
        while (r<=right)
        {
            temp[t]=arr[r];
            t++;
            r++;
        }
        int tempL = left;
        t = 0;
        System.out.println("tempL=" + tempL + "; right=" + right);
        while (tempL<=right)
        {
            arr[tempL] = temp[t];
            tempL++;
            t++;
        }

    }
    public static void mergeSort(int []arr,int left,int right,int[] temp)
    {
        if(left<right)
        {
            int mid = (left+right)/2;
            mergeSort(arr,left,mid,temp);
            mergeSort(arr,mid+1,right,temp);
            merge(arr,left,mid,right,temp);
        }
    }

    public static void main(String[] args) {
        int []arr= {8, 4, 5, 7, 1, 3, 6, 2};
        int []temp = new int[arr.length];
        mergeSort(arr,0,arr.length-1,temp);
        System.out.println(Arrays.toString(arr));
    }
}
